package com.example.lancooznbk.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.adapter.ViewPagerAdapter;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.fragment.AboutMeFragment;
import com.example.lancooznbk.view.fragment.BaseReviewFragment;
import com.example.lancooznbk.view.fragment.GuessTestingFragment;
import com.example.lancooznbk.view.fragment.DifficultReviewFragment;
import com.lancoo.cpbase.authentication.base.ExitBack;
import com.lancoo.cpbase.authentication.base.InfoListener;
import com.lancoo.cpbase.authentication.base.LoginBack;
import com.lancoo.cpbase.authentication.base.LoginOperate;
import com.lancoo.cpbase.authentication.base.TokenListener;
import com.lancoo.cpbase.authentication.base.TokenManager;
import com.lancoo.reviewsystem.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity{

    private LoginOperate mOperate;// 登录操作工具类

    public static boolean[] bCouldPlay;

    private ViewPager mViewPager;
    private ViewPagerAdapter mViewPagerAdapter;
    private RadioGroup mRadioGroup;

    private boolean isShowMenu = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int attachLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {

        mOperate = new LoginOperate(this);
        //用户令牌失效回调接口
        TokenManager.getInstance().addTokenListener(tokenListener);
        TokenManager.getInstance().setInfoListener(infoListener);//用户信息更新回调接口,该接口在登录，登出和被调用getCurrentUserinfo(String address)方法(注：需要将getCurrentUserinfo方法放置在新的线程中执行)后会被回调使用


        mRadioGroup = (RadioGroup) findViewById(R.id.rg_tab);

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb_baseReview:
                       mViewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_impAndDifTutor:
                        mViewPager.setCurrentItem(1);
                        break;
                    case R.id.rb_guessTesting:
                        mViewPager.setCurrentItem(2);
                        break;
                    case R.id.rb_me:
                        mViewPager.setCurrentItem(3);
                        break;
                }
            }
        });

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new BaseReviewFragment());
        fragments.add(new DifficultReviewFragment());
        fragments.add(new GuessTestingFragment());
        fragments.add(new AboutMeFragment());
        List<String> titles = new ArrayList<>();
        titles.add("基本复习");
        titles.add("重难点辅导");
        titles.add("猜题测试");
        titles.add("我");
        mViewPager = (ViewPager) findViewById(R.id.vp_main);
        mViewPager.setOffscreenPageLimit(fragments.size());
        mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPagerAdapter.setItems(fragments,titles);
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
/*

                    for (int i = 0; i < mViewPagerAdapter.getCount(); i++) {
                        if (i == 0 || i == 1) {
                            mViewPagerAdapter.getItem(i).setHasOptionsMenu(i == position);
                        }else {
                            mViewPagerAdapter.getItem(i).setHasOptionsMenu(false);
                        }

                    }
*/



                ((RadioButton)mRadioGroup.getChildAt(position)).setChecked(true);
               // refreshMenu(position);
            }
        });


    }

    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        //menu.findItem(R.id.item_other_system).setVisible(isShowMenu);
        return true;
    }*/

    @Override
    protected void updateViews(boolean isRefresh) {

    }



    private void refreshMenu(int currentItem){
        switch (currentItem){
            case 0:
            case 1:
                isShowMenu = true;
                break;
            case 2:
            case 3:
                isShowMenu = false;
                break;
        }

        supportInvalidateOptionsMenu();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        TokenManager.getInstance().removeTokenListener(tokenListener);
        TokenManager.getInstance().setInfoListener(null);
        mOperate.stopService();
    }

    /*
     * *************身份认证监听、用户信息监听**************
     */
    private TokenListener tokenListener = new TokenListener() {
        @Override
        public void tokenInvalid(int flag) {
            if (flag == 0) {// 用户失效
                runOnUiThread(new Runnable() {
                    public void run() {
                        mOperate.openLoginDialog(new LoginBack() {
                            @Override
                            public void loginSuccess() {// 登录成功

                            }
                        }, false, false, new ExitBack() {
                            @Override
                            public void exit() {

                            }
                        });
                    }
                });
            } else if (flag == 1) {// 其他应用退出导致失效
                new Runnable() {
                    public void run() {

                    }
                };
            }
        }
    };
    // 用户信息更新回调
    private InfoListener infoListener = new InfoListener() {
        @Override
        public void infoRefresh() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                }
            });
        }
    };


    //按两次退出应用
    private long mExitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {

            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                if ((System.currentTimeMillis() - mExitTime) > 2000) {
                    Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                } else {
                    finish();
                    System.exit(0);//完全退出程序
                }
            }


            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



}
